//
//  GYGoodsEvaluateCell.m
//  GoodLuck
//
//  Created by apple on 2018/3/10.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *pictureCollectionViewCellIdentifier = @"pictureCollectionViewCellIdentifier";

static NSString *goodsEvaluateTbaleViewCellIdentifier = @"goodsEvaluateTbaleViewCellIdentifier";

#import "GYGoodsEvaluateCell.h"
#import "GYGoodEvaluateCollectionViewCell.h"
#import "RatingBar.h"

@interface GYGoodsEvaluateCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIImageView *userHeadImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLable;
@property (weak, nonatomic) IBOutlet UILabel *timeLable;
@property (weak, nonatomic) IBOutlet UILabel *evaluateContentLable;
@property (weak, nonatomic) IBOutlet UICollectionView *pictureCollectionView;
//@property (weak, nonatomic) IBOutlet UILabel *browseLable;

//图片显示的视图的高度约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureCollectionViewHeightCons;

@property (weak, nonatomic) IBOutlet UILabel *bgLable;

/** 商品评分 */
@property (nonatomic, strong) RatingBar *goodsScoreView;
@end
@implementation GYGoodsEvaluateCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self.pictureCollectionView registerNib:[UINib nibWithNibName:@"GYGoodEvaluateCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:pictureCollectionViewCellIdentifier];
    self.pictureCollectionView.bounces = NO;
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //添加评价新图
    RatingBar *goodsScoreView = [[RatingBar alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.userNameLable.frame)+10, self.userNameLable.y, 100, self.userNameLable.height)];
    
    goodsScoreView.enable = NO;
    [self addSubview:goodsScoreView];
    self.goodsScoreView = goodsScoreView;
}




+ (instancetype)goodsEvaluateCellWithTableView:(UITableView *)tableView {
    GYGoodsEvaluateCell *cell = [tableView dequeueReusableCellWithIdentifier:goodsEvaluateTbaleViewCellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] firstObject];
    }
    
    return cell;
}

- (void)setGoodsEvaluateModel:(GYGoodsEvaluate *)goodsEvaluateModel {
    _goodsEvaluateModel = goodsEvaluateModel;
    
    //赋值
    [self.userHeadImageView sd_setImageWithURL:[NSURL URLWithString:goodsEvaluateModel.member_avatar] placeholderImage:[UIImage imageNamed:@"ff_icon4-1"]];
    self.userNameLable.text = goodsEvaluateModel.geval_frommembername;
    self.timeLable.text = [[goodsEvaluateModel.date componentsSeparatedByString:@" "] firstObject];
    self.evaluateContentLable.text = goodsEvaluateModel.geval_content;
//    self.browseLable.text = @"浏览573人";
    self.goodsScoreView.starNumber = [goodsEvaluateModel.geval_scores intValue];
    
    
    //这里要还要设置collectionView的高度
    CGFloat itemW = (ScreenWidth-50)/3;
    NSUInteger imageCount = goodsEvaluateModel.geval_image_240.count/3;
    NSUInteger index = goodsEvaluateModel.geval_image_240.count%3;
    
    if (index == 0) {
        self.pictureCollectionViewHeightCons.constant =  itemW*imageCount + imageCount <= 1 ? 0 :(imageCount-1)*10;
    }else {
        self.pictureCollectionViewHeightCons.constant = itemW*(imageCount+1) + imageCount*10;
    }
    
    //刷新表格
    [self.pictureCollectionView reloadData];
}

- (CGFloat)cellHeight:(GYGoodsEvaluate *)model {
    self.goodsEvaluateModel = model;
    
    [self layoutIfNeeded];
    
    return CGRectGetMaxY(self.bgLable.frame);
}

#pragma mark -- UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.goodsEvaluateModel.geval_image_240.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYGoodEvaluateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:pictureCollectionViewCellIdentifier forIndexPath:indexPath];
    NSString *image_url = self.goodsEvaluateModel.geval_image_240[indexPath.item];
    [cell.evaluateImageView sd_setImageWithURL:[NSURL URLWithString:image_url] placeholderImage:[UIImage imageNamed:@"4"]];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((ScreenWidth-50)/3, (ScreenWidth-50)/3);
}



@end
